﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Setup.Interfaces;
using System.Windows;
using Presentation.Module.Setup.Interfaces.ViewModels;
using Presentation.Module.Setup.Interfaces.Services;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Presentation.Module.Setup.Models;
using System.ComponentModel;
using Presentation.Module.Setup.Interfaces.Views;
using Microsoft.Practices.Prism.Regions;
using System.Collections.ObjectModel;

namespace Presentation.Setup.Views
{
    public class TaskCategoriesViewModel : ITaskCategoriesViewModel, INotifyPropertyChanged
    {
        private ObservableCollection<Presentation.Module.Setup.Models.Skill> _Skills;

        // Constructor injection, injecting a skill service instance
        public TaskCategoriesViewModel(ISkillsService skillService, IEditSkillView editSkillView, IAddSkillView addSkillView, IRegionManager regionManager)
        {
#warning add dependency validation
            this.SkillService = skillService;
            this.RegionManager = regionManager;

            //Views
            EditSkillView = editSkillView;
            AddSkillView = addSkillView;

            this.RegionManager.Regions["MainRegion"].Add(EditSkillView);
            this.RegionManager.Regions["MainRegion"].Add(AddSkillView);

            EditSkillView.ViewExit += AddEditSkillView_ViewExit;
            AddSkillView.ViewExit += AddEditSkillView_ViewExit;

            // Wire up commands to implementations
            this.EditSkill = new DelegateCommand<Skill>(OnEditSkill);
            this.AddSkill = new DelegateCommand<Skill>(OnAddSkill);
            this.RemoveSkill = new DelegateCommand<Skill>(OnRemoveSkill);

            // Get all the skills and store in a property
            this.Skills = new ObservableCollection<Skill>(skillService.GetAllSkills());
        }      

        // List of skills - View binds to these
        public ObservableCollection<Presentation.Module.Setup.Models.Skill> Skills
        {
            get
            {
                return _Skills;
            }
            set
            {
                if (value != null)
                {
                    _Skills = value;
                    RaiseOnPropertyChanged("Skills");
                }
            }
        }

        #region Injected Dependencies
        public ITaskCategoriesView View { get; set; }
        public IEditSkillView EditSkillView { get; set; }
        public IAddSkillView AddSkillView { get; set; }
        public IRegionManager RegionManager { get; set; } 
        #endregion

        #region Commands - View binds to these for behaviour
        public ICommand EditSkill { get; private set; }
        public ICommand AddSkill { get; private set; }
        public ICommand RemoveSkill { get; private set; }
        #endregion

        private ISkillsService SkillService { get; set; }

        void AddEditSkillView_ViewExit()
        {
            this.RegionManager.Regions["MainRegion"].Activate(this.View);
            this.Skills = new ObservableCollection<Skill>(SkillService.GetAllSkills());            
        }

        #region Command implementations
        private void OnEditSkill(Skill skill)
        {
            EditSkillView.EditSkill(skill);

            this.RegionManager.Regions["MainRegion"].Activate(EditSkillView);

            // Not the most efficent way, but Meh!
            this.Skills = new ObservableCollection<Skill>(SkillService.GetAllSkills());
        }

        private void OnAddSkill(Skill skill)
        {
            AddSkillView.AddSkill();

            this.RegionManager.Regions["MainRegion"].Activate(AddSkillView);

            // Not the most efficent way, but Meh!
            this.Skills = new ObservableCollection<Skill>(SkillService.GetAllSkills());
        }

        private void OnRemoveSkill(Skill skill)
        {
            if (MessageBox.Show("Are you sure that you want to delete this item?", "Delete Skill", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                SkillService.RemoveSkill(skill);
                this.Skills = new ObservableCollection<Skill>(SkillService.GetAllSkills());
            }
        }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
